/**
 * 获取路由参数
 * @Author: linzy
 * @Date: 2022-05-27

 * 返回路由参数
 * 1. 默认会合并成一个对象
 * 2. 如果有相同的参数名字，导致返回的对象属性合并了，那请拿取里面的params跟search
 * @description 获取路由参数
 * @function useParams
 * @param {object} props - 页面的props对象
 * @returns {object} paramsObj
 * @example
const { id } = useParams(props);
 */
import { useMemo } from "react";
import qs from "qs";
import _ from 'lodash';

export default props => {
  var href = window.location.href;
  const paramsObj = useMemo(() => {
    const params = props.match.params;
    const search = qs.parse(props.location.search.slice(1));
    _.forEach(search, (val, key, obj) => {
      if (/^[1-9]\d*$/.test(val) && val.length < 18) {
        obj[key] = +val;
      }
    })
    const result = { ...params, ...search, params, search };
    for (const [key, val] of Object.entries(result)) {
      if (/^[1-9]\d*$/.test(val) && val.length < 18) {
        result[key] = +val;
      }
    }
    return result;
  }, [href]);
  return paramsObj;
};
